﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RelationExpertSystem.Relation
{
    public class Cousin : RelationBase
    {
        private static Cousin _singleton = new Cousin();
        public static Cousin Instance
        {
            get { return _singleton; }
        }

        private Cousin() { }

        public override bool IsRelated(Person a, Person b)
        {
            // b is a's cousin IFF one of a's parents and one of b's parents are siblings
            return CheckSiblings(a.Mother, b.Mother) ||
                CheckSiblings(a.Mother, b.Father) ||
                CheckSiblings(a.Father, b.Mother) ||
                CheckSiblings(a.Father, b.Father);
        }

        private bool CheckSiblings(Person a, Person b)
        {
            if (!Person.IsNull(a) && !Person.IsNull(b))
                return a.KnownRelation(Sibling.Instance, b);

            return false;
        }

        public override IEnumerable<IRelation> GetDependencies()
        {
            return new HashSet<IRelation>(new IRelation[] { Sibling.Instance });
        }
    }
}
